package com.aiju.cash.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Shop;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.ui.login.manager.LoginCheck;
import com.aiju.utils.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-11-10 14:42
 * @version: V1.0
 * @Description: 注册页面
 */
public class RegisterActivity extends BaseTitleActivity {

    private EditText userNameEditText;
    private EditText passWordEditText;

    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setCenterTitle("创建注册");
        userName = getIntent().getStringExtra("userName");

        userNameEditText = (EditText) findViewById(R.id.et_register_name);
        passWordEditText = (EditText) findViewById(R.id.et_register_password);
        findViewById(R.id.tv_register_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String nickName = userNameEditText.getText().toString().trim();
                final String passWord = passWordEditText.getText().toString().trim();

                if (LoginCheck.checkPassword(passWord) && LoginCheck.checkUserName(nickName)) {
                 mDialogHelper.showLoadingProgressDialog();
                    new LoginApi().register(userName, nickName, passWord, new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            T.ToastNetError();
                            mDialogHelper.dismissProgressDialog();
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            mDialogHelper.dismissProgressDialog();

                            DataJson dataJson = new DataJson(response);
                            if (dataJson.isSuccess()) {
                                T.ToastShort("注册成功");
                                Shop shop = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<Shop>() {}.getType());
                                registerSuccess(shop.getId(), userName, passWord);
                            } else {
                                T.ToastShort(dataJson.getMsg());
                            }
                        }
                    });
                }

            }
        });
    }

    /**
     * 注册成功
     * @param shopId
     * @param userName
     * @param passWord
     */
    private void registerSuccess(String shopId, String userName, String passWord) {
        Intent intent = new Intent(RegisterActivity.this, CreateShopActivity.class);
        intent.putExtra("shopId", shopId);
        intent.putExtra("userName", userName);
        intent.putExtra("password", passWord);
        startActivity(intent);
    }
}
